package com.study.hot.jetpack.lifecycle

import android.content.Context
import android.os.SystemClock
import android.util.AttributeSet
import android.widget.Chronometer
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent

/**
 * <pre>
 *     author : fauks
 *     time   : 2020/03/25
 *     desc   :
 * </pre>
 */
class CustomChronometer(context: Context?, attrs: AttributeSet?) : Chronometer(context, attrs), LifecycleObserver {
    var watchTime = 0L


    //绑定Activity声明周期
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onStart() {
        base = SystemClock.elapsedRealtime() - watchTime
        start()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onStop() {
        watchTime = SystemClock.elapsedRealtime() - base
        stop()
    }
}